package top.yangbuyi.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: AddressUtils
 * @create: 2020-08-27 21:12
 * @author: yangbuyi
 * @since： JDK1.8
 * @AddressUtils: 获取地址工具类$根据ID获取真实的地址
 **/
public class AddressUtils {
	  private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

	  public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";

	  public static String getRealAddressByIP(String ip) {
			String address = "XX XX";
			// 内网不查询
			if (IpUtils.internalIp(ip)) {
				  return "内网IP";
			}

			String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip);
			if (StringUtils.isEmpty(rspStr)) {
				  log.error("获取地理位置异常 {}", ip);
				  return address;
			}
			JSONObject obj = JSONObject.parseObject(rspStr);
			JSONObject data = obj.getObject("data", JSONObject.class);
			String region = data.getString("region");
			String city = data.getString("city");
			address = region + " " + city;
			return address;
	  }
}

